﻿using AutoMapper;
using MediatR;
using Traceability.API.Applications.Command.AquacultureCommand;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.AquacultureCommandHandler
{
    public class HealthcareAddCommandHandler : IRequestHandler<HealthcareAddCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Healthcare_Information> _healthcareRepository;
        private readonly IMapper mapper;

        public HealthcareAddCommandHandler(IBaseRepository<Healthcare_Information> healthcareRepository, IMapper mapper)
        {
            _healthcareRepository = healthcareRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(HealthcareAddCommand request, CancellationToken cancellationToken)
        {
           var model=mapper.Map<Healthcare_Information>(request);
            
            int count =await _healthcareRepository.Add(model);

            APIResult<int> aPIResult = new APIResult<int>();
            if (count>0)
            {
                aPIResult.Code=EnumCode.Ok;
                aPIResult.Msg = "添加成功";
                aPIResult.Data = count;
            }
            else
            {
                aPIResult.Code = EnumCode.File;
                aPIResult.Msg = "添加失败";
            }

            return await Task.FromResult(aPIResult);
        }
    }
}
